package com.lytoyo.neihanduanzi.config;

import com.lytoyo.neihanduanzi.utils.Constant;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

/**
 * Package:com.lytoyo.neihanduanzi.config
 *
 * @ClassName:RabbitMqConfig
 * @Create:2025/2/27 15:30
 **/
@Configuration
public class RabbitMqConfig {
    /**
     * 确保消息以JSON格式序列化/反序列化。
     * @return
     */
    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 开机创建交换机
     * @return
     */
    @Bean
    public DirectExchange messageExchange() {
        return new DirectExchange(Constant.EXCHANGE_DIRECT_USER_MESSAGE, true, false);
    }

    /**
     * 用于运行时动态管理队列和绑定
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        return new RabbitAdmin(connectionFactory);
    }
}
